package com.hyt.it.ogt.kq.service.bm.pay;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;


/**
 *http请求request转换
 * @author wu
 *
 */
public class HttpParamUtil {
	

	

    private HashMap<String,String> params;
    
    /**
     * request转实体类
     * @param <T>
     * @param req
     * @return
     */
    public static <T>T req2Obj(HttpServletRequest req,Class<?> clazz){
    	Map<String, Object> reqMap = HttpParamUtil.getParams(req);
		// 第二步，map转实体
		return  (T) MapUtils.map2Object(reqMap, clazz);
    } 

    public HttpParamUtil(HttpServletRequest req){

        try{
            //只解析GET提交的数据
            if("GET".equals(req.getMethod())){
                params = new HashMap<String, String>();
                //解析queryString
                String queryString = req.getQueryString();
                String[] ps = queryString.split("&");
                String[] p = null;
                for(String param : ps){
                    p = param.split("=");
                    if(p.length==2){
                        params.put(p[0], URLDecoder.decode(p[1], "GBK"));
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

//
//    private String getParameter(String key){
//
//        if(params != null){
//            return params.get(key);
//        }else{
//            return "";
//        }
//    }
    
    /**
     * request 解析成map
     * @param request
     * @return
     */
    private static Map<String , Object> getParams(HttpServletRequest request){
    	Map<String, Object> paramsMap = new HashMap<String, Object>();
        try{
            //只解析GET提交的数据
            if("GET".equals(request.getMethod())){
                //解析queryString
                String queryString = request.getQueryString();
                String[] ps = queryString.split("&");
                String[] p = null;
                for(String param : ps){
                    p = param.split("=");
                    if(p.length==2){
                    	paramsMap.put(p[0], URLDecoder.decode(p[1], "UTF-8"));
                    }
                }
            }else {
            	Map<String, String[]> requestArrayMap = request.getParameterMap();
            	for (String key : requestArrayMap.keySet()) {
            		paramsMap.put(key, requestArrayMap.get(key)[0]);
        		}
            }
            return paramsMap;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }

    }
    
	/**
	 * 遍历实体类，并将参数都按格式decode
	 * 
	 * @param obj
	 * @param coder 编码集UTF-8,GBK
	 * @throws Exception
	 */
	public static <T> T obj2decode(Object obj,String coder) {

		try {
			Class cls = obj.getClass();
			Field[] fields = cls.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field f = fields[i];
				f.setAccessible(true);
				if (f.get(obj) == null || f.get(obj) == "") {
					// donothing
				} else {
					String decoderStr = URLDecoder.decode(f.get(obj).toString().trim(), coder);
					decoderStr = decoderStr.replace(" ", "+");
					f.set(obj, decoderStr);
				}

				// System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(obj));
			}
		} catch (Exception e) {
			obj = null;
			e.printStackTrace();
		}

		return (T) obj;
	}

}
